﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEET.LEETCommon.Model;
using LEET.LEETCommon;
using System.IO;
using LEET.CommonInterfaces.TestControls;
using System.Diagnostics;
using Compiled_Tests.ProcessWrappers;
using Compiled_Tests;

namespace LEET.StandardModuals.Instructions
{
    public class Start : Instruction
    {
        private String exe_path;
        private String arguments;
        private String name;

        public Start(string [] args)
            : base(args)
        {
            exe_path = args[1].Trim().Replace("\\", "\\\\");
            int len = args.Length;
            if (len >= 2 && args[len - 2].Trim() == "as")
            {
                name = args[len - 1].Trim();
                len -= 2;
            }
            arguments = (len > 2) ? args[2].Trim().Replace("\\", "\\\\").Replace("\"", "\\\"") : null;
            InstructionType = NodeTypes.START_TYPE;
            Name = Path.GetFileName(exe_path.Trim());
        }

        public static IAutomationActionResult StaticCall(String exe, String args, String name)
        {
            Process process = LEET.LEETCommon.Utilities.Start_Process(exe, args);
            return new AutomationActionResult(new ProcessWrapper(process, name));
        }

        public static IAutomationActionResult StaticCall(String exe, String args, IBox box)
        {
            Process process = LEET.LEETCommon.Utilities.Start_Process(exe, args);
            return new AutomationActionResult(new ProcessWrapper(process, box.MakeUnique(process.ProcessName)));
        }

        public override String RenderStaticCall()
        {
            return "LEET.StandardModuals.Instructions.Start.StaticCall(\"" + exe_path + "\", \"" + arguments + "\", " + ((name != null) ? ("\"" + name + "\"") : "Box") + ")";
        }


        public override string PlainText()
        {
            return "Started " + exe_path + ".";
        }
    }
}
